package utils

import (
	"fmt"
	"gitee.com/wengo/windows_gui/bkmanager"
	"gitee.com/wengo/windows_gui/widget"
	"gitee.com/wengo/windows_gui/xc"
	"gitee.com/wengo/windows_gui/xcc"
	"math"
	"strconv"
	"unsafe"
)

func createPageButton(layoutHandle, id int) *widget.Button{
	pageBtn := widget.NewButton(0, 0, 0, 0, fmt.Sprintf("page%d", id), layoutHandle)

	bkm := bkmanager.NewBkManager()
	//bkm.SetBkInfo("{99:1.9.9;98:272(0)288(0)320(0)144(1)160(1)192(1);5:2(15)20(1)21(3)26(0)22(-12764364)23(255);5:2(15)20(1)21(3)26(0)22(-6193386)23(255);}")
	bkm.SetBkInfo("{99:1.9.9;98:272(0)288(0)320(0)144(1)160(1)192(1);5:2(15)20(1)21(3)26(0)22(-12435399)23(255);5:2(15)20(1)21(3)26(0)22(-6193386)23(255);}")
	pageBtn.SetBkManager(bkm.Handle)
	pageBtn.SetWidth(32)
	pageBtn.SetHeight(32)
	pageBtn.SetText(strconv.Itoa(id))
	pageBtn.SetID(id)
	is64 := unsafe.Sizeof(uintptr(0)) == 8
	if is64 {
		pageBtn.SetCursor(65567)
	} else {
		pageBtn.SetCursor(32649)
	}
	pageBtn.SetTypeEx(xcc.Button_Type_Radio)
	pageBtn.SetTextAlign(xcc.TextAlignFlag_Center|xcc.TextAlignFlag_Vcenter)
	pageBtn.SetStyle(xcc.Button_Style_Default)

	pageBtn.SetTextColor(xc.ABGR(255, 255,255,255))
	//pageBtn.SetTextAlign(xcc.TextAlignFlag_Center)
	return pageBtn
}

func Pagination(hAdapter int, elementName string, inventoryListBox *widget.ListBox) {
	inventoryPageLayout := widget.NewLayoutEleByName(elementName)
	//page1 := widget.NewButtonByHandle(xc.XC_GetObjectByName("page1"))

	total := 83
	for i:= 1; i < 10; i++ {

		/*testBtn := widget.NewButton(0, 0, 0, 0, fmt.Sprintf("page%d", i), inventoryPageLayout.Handle)
		testBtn.SetBkManager(page1.GetBkManager())
		testBtn.SetWidth(32)
		testBtn.SetHeight(32)
		testBtn.SetText(strconv.Itoa(i))
		testBtn.SetID(i)
		testBtn.SetCursor(page1.GetCursor())
		testBtn.SetType(xcc.Button_Type_Radio)
		testBtn.SetTextColor(xc.RGB(255, 255, 255), 255)
		*/
		pageBtn := createPageButton(inventoryPageLayout.Handle, i)

		numPages := total
		numLinks := 8
		pageBtn.Event_BnClick(func(pbHandled *bool) int {
			*pbHandled = true
			var start int
			var end int

			page, _ := strconv.Atoi(pageBtn.GetText())
			if numPages <= numLinks {
				start = 1
				end = numPages
			} else {
				start = page - int(math.Floor(float64(numLinks/2)))
				end = page + int(math.Floor(float64(numLinks/2)))

				if start < 1 {
					end += int(math.Abs(float64(start))) + 1
					start = 1
				}

				if end > numPages {
					start -= end - numPages
					end = numPages
				}
			}

			for j := 1; j < 10; j++ {
				if start <= end {
					xc.XBtn_SetText(inventoryPageLayout.GetChildByID(j), strconv.Itoa(start))
					if start == page {
						xc.XBtn_SetCheck(inventoryPageLayout.GetChildByID(j), true)
					}
					fmt.Println(xc.XBtn_GetText(inventoryPageLayout.GetChildByID(j)), page)
					start++
				}

			}

			xc.XAdTable_DeleteItemAll(hAdapter)
			for i:=1; i < 10; i++ {
				item := xc.XAdTable_AddItemText(hAdapter, "Pool & Spa Parts & Accessories2")
				xc.XAdTable_SetItemText(hAdapter, item, 1, "B01AASAM7M")
				xc.XAdTable_SetItemText(hAdapter,item, 2, "2021-11-2")
				xc.XAdTable_SetItemText(hAdapter,item, 3, "2021-11-3")
			}
			inventoryListBox.Redraw(true)
			inventoryPageLayout.Redraw(true)
			return 0
		})
	}
}
